//
//  BChargeCashSegmentView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/7.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BChargeCashSegmentView.h"

@interface BChargeCashSegmentView ()

@property (nonatomic,  copy) NSArray *segmentItems;

@end

@implementation BChargeCashSegmentView

- (NSArray *)segmentItems {
    return @[BCLocalizedKey(@"Charge"),BCLocalizedKey(@"Cash")];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setsupSubviews];
    }
    return self;
}
- (void)setsupSubviews {
    self.segment = [[UISegmentedControl alloc] initWithItems:self.segmentItems];
    [self addSubview:self.segment];
    [self.segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    //setting
    self.segment.tintColor = [UIColor clearColor];
    [self.segment setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18 weight:UIFontWeightMedium],NSForegroundColorAttributeName:[UIColor qmui_colorWithHexString:@"#999999"]} forState:UIControlStateNormal];
    [self.segment setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18 weight:UIFontWeightMedium],NSForegroundColorAttributeName:[UIColor darkMainColor]} forState:UIControlStateSelected];
//    [self.segment addTarget:self action:@selector(segmentValueChangedAction:) forControlEvents:UIControlEventValueChanged];
    
    //
    self.selectBarView = [UIView new];
    self.selectBarView.backgroundColor = [UIColor darkMainColor];
    [self addSubview:self.selectBarView];
    [self.selectBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(0);
    }];
}

//layout
- (void)layoutSelectBar:(UISegmentedControl *)segment {
    NSInteger selectIndex = segment.selectedSegmentIndex;
    //    CGFloat width = [segment wi]
    NSString *title = [segment titleForSegmentAtIndex:selectIndex];
    //
    NSDictionary *attributes = [segment titleTextAttributesForState:UIControlStateSelected];
    //
    CGSize titleSize = [title sizeWithAttributes:attributes];
    //
    CGFloat singleWidth = CGRectGetWidth(self.frame)/segment.numberOfSegments;
    CGFloat offset = singleWidth*selectIndex+singleWidth/2;
    //
    [self.selectBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(titleSize.width+18.0);
        make.height.mas_equalTo(2);
        make.bottom.mas_equalTo(self);
        make.centerX.mas_equalTo(self.mas_left).offset(offset);
    }];
}



@end
